%{
let fold_activities f al = List.fold_right f al
let fold_if = List.fold_left Contract.mk_if
%}

%token <string> AZIONE
%token OPENGRAF CLOSEGRAF EOL 
%token EMPTY SEQUENCE REPEAT WHILE IF FLOW PICK

%start main
%type <Contract.activity> main
%%

main:
	activity EOL		{$1}
;

activity:
	 EMPTY						{Contract.One}
	|AZIONE						{Contract.Action($1)}
	|SEQUENCE OPENGRAF activity_list CLOSEGRAF	{fold_activities Contract.mk_sequence $3 Contract.One}
	|REPEAT OPENGRAF activity CLOSEGRAF             {Contract.Repeat $3}
	|WHILE OPENGRAF activity CLOSEGRAF		{Contract.While($3)}
	|IF OPENGRAF activity activity_list CLOSEGRAF	{fold_if $3 $4}
	|FLOW OPENGRAF activity_list CLOSEGRAF 		{fold_activities Contract.mk_flow $3 Contract.One}
	|PICK OPENGRAF activity_list CLOSEGRAF 		{fold_activities Contract.mk_pick $3 Contract.Zero}
;

activity_ne_list:
	 activity {[$1]} 
	|activity activity_ne_list {$1::$2}
;

activity_list:
	   {[]}
	|activity_ne_list {$1}
;












